<?php
include_once("ConnectionManager.php");

function findAllSpecialProducts(){
	$query = "SELECT SP.SPRO_ID,(SELECT P.PRO_CODE FROM MITI_PRODUCT P WHERE P.PRO_ID = SP.SPRO_PRO_ID) AS PRO_CODE, (SELECT P.PRO_NAME FROM MITI_PRODUCT P WHERE P.PRO_ID = SP.SPRO_PRO_ID) AS PRO_NAME, (SELECT P.PRO_PRICE FROM MITI_PRODUCT P WHERE P.PRO_ID = SP.SPRO_PRO_ID)  AS PRO_PRICE, SPRO_DESCRIPTION, SPRO_STATUS FROM MITI_SPECIAL_PRODUCT SP";
	ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function findSpecialProductBy($speProId){
    $query = "SELECT SPRO_ID, SPRO_PRO_ID, SPRO_DESCRIPTION, SPRO_STATUS, 
                    (SELECT CAT_ID FROM MITI_PRODUCT WHERE PRO_ID = SPRO_PRO_ID) AS CAT_ID
              FROM MITI_SPECIAL_PRODUCT WHERE SPRO_ID = $speProId";
    ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row;
}

function findSpecialProductByNormalProduct($proId){
	$query = "SELECT SPRO_ID, SPRO_PRO_ID, SPRO_DESCRIPTION, SPRO_STATUS
              FROM MITI_SPECIAL_PRODUCT WHERE SPRO_PRO_ID = $proId  AND SPRO_STATUS = TRUE";
    ConnectionManager::getInstance()->setQuery($query);
    $result = ConnectionManager::getInstance()->query();
	if(mysql_num_rows($result) > 0)
		return mysql_fetch_array($result);
	else
		return null;
}

function updateSpecialProduct($speProId, $proId, $description, $status){
    $query = "UPDATE MITI_SPECIAL_PRODUCT SET 
                SPRO_PRO_ID = $proId, SPRO_DESCRIPTION = '$description', SPRO_STATUS = $status WHERE SPRO_ID = $speProId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows() > 0;
}

function updateSpecialProductStatus($speProId, $status){
    $query = "UPDATE MITI_SPECIAL_PRODUCT SET SPRO_STATUS = $status WHERE SPRO_ID = $speProId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows() > 0;
}

function createSpecialProduct($proId, $description, $status){
    $query = "INSERT INTO MITI_SPECIAL_PRODUCT (SPRO_PRO_ID, SPRO_DESCRIPTION, SPRO_STATUS) VALUES ($proId, '$description', $status)";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_insert_id();
}

function updateInactiveExcept($speProId){
    $query = "UPDATE MITI_SPECIAL_PRODUCT SET SPRO_STATUS = 0 WHERE SPRO_ID <> $speProId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_affected_rows() > 0;
}

function findImagesOfActiveSpecialProduct(){
    $query = "SELECT SPRO_ID, IMG_PATH FROM MITI_IMAGE WHERE SPRO_ID IN (SELECT SPRO_ID FROM MITI_SPECIAL_PRODUCT WHERE SPRO_STATUS = 1)";
    ConnectionManager::getInstance()->setQuery($query);
    return ConnectionManager::getInstance()->query();
}

function findActiveSpecialProduct(){
	$query = "SELECT SPRO_PRO_ID, (SELECT PRO_NAME FROM MITI_PRODUCT WHERE PRO_ID = SPRO_PRO_ID) AS NAME FROM MITI_SPECIAL_PRODUCT WHERE SPRO_STATUS = 1";
	ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row;
}

function isSpecialProduct($proId){
		$query = "SELECT COUNT(*) AS COUNT
				  FROM MITI_PRODUCT
				  WHERE PRO_ID IN (SELECT SPRO_PRO_ID FROM MITI_SPECIAL_PRODUCT WHERE SPRO_STATUS = 1) AND PRO_ID = $proId";
	ConnectionManager::getInstance()->setQuery($query);
    $row = ConnectionManager::getInstance()->fetch(ConnectionManager::getInstance()->query());
    return $row["COUNT"] > 0;
}


function createSpecialImage($speProId, $path){
    $query = "INSERT INTO MITI_IMAGE(SPRO_ID, IMG_PATH) VALUES ($speProId, '$path')";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
    return mysql_insert_id();
}

function deleteImageOfSpecialProduct($imagePath, $speProId){
    $query = "DELETE FROM MITI_IMAGE WHERE IMG_PATH = '$imagePath' and SPRO_ID = $speProId";
    ConnectionManager::getInstance()->setQuery($query);
    ConnectionManager::getInstance()->query();
}

?>